<template>
  <multiple-dept-tree
    :checkList="checkList"
    v-if="tree.length > 0"
    keyValue="deptId"
    :treeProp="prop"
    @sendValue="sendValue"
    :isCheck="true"
    :treeNone="tree"
    :multiple="multiple"
    :deptId="deptId"
    :excludeIds="excludeIds"
  ></multiple-dept-tree>
</template>

<script>
import MultipleDeptTree from "./multiple-dept-tree/tree.vue";
import { listDeptChildren, getDept, listDeptTree } from "@/api/system/dept.js";
export default {
  name: "MultipleUserSelector",
  components: {
    MultipleDeptTree,
  },
  data() {
    return {
      searchKey: "",
      listData: [],

      tree: [], // 树形数据
      prop: {
        label: "label", // 树形展示字段label
        children: "children", // 树形子级字段children
        checkStrictly: true,
      },
    };
  },
  props: {
    checkList: {
      type: [Number, Array, String],
    },
    multiple: {
      type: Boolean,
      default: false,
    },
    deptId: {
      type: [String, Number],
      default: "",
    },
    // 排除的用户ID
    excludeIds: {
      type: Array,
      default: () => {
        return [];
      },
    },
  },
  mounted() {
    this.tree = [];
    this.getDept();
  },
  methods: {
    sendValue(data) {
      this.$emit("change", data);
    },
    getDept() {
      // 缓存部门数据（60s）
      listDeptChildren().then((res) => {
        this.addDeptIdToTree(res.data || []);
        this.$simpleCache.put("deptTree", res.data, 60);
        let tree = res.data;
        if (this.deptId) {
          this.tree = this.getDeptTree(tree, this.deptId);
        } else {
          this.tree = tree;
        }
      });
    },
    getDeptTree(arr, deptId) {
      for (const node of arr) {
        if (node.deptId == deptId) {
          return [node];
        }
        if (node.children) {
          const res = this.getDeptTree(node.children, deptId);
          if (res) return res;
        }
      }
      return [];
    },
    // 转换成树形结构
    arrToTree(arr, parentId = 0) {
      const res = [];
      arr.forEach((item) => {
        item.user = false;
        if (item.parentId === parentId) {
          const children = this.arrToTree(
            arr.filter((v) => v.parentId !== parentId),
            item.deptId
          );
          if (children.length) {
            res.push({
              ...item,
              children,
            });
          } else {
            res.push({
              ...item,
            });
          }
        }
      });
      return res;
    },
    // tree data添加deptId字段, 来兼容组件
    addDeptIdToTree(originData = []) {
      function loop(data = []) {
        data.map((item) => {
          if (!item.deptId) {
            item.deptId = item.id;
          }
          if (item.children) {
            loop(item.children);
          }
        });
      }
      loop(originData);
    },
  },
};
</script>
<style lang="scss" scoped>
.example-body {
  padding: 10px;
}

.scroll-view {
  // padding-top: var(--status-bar-height);
  /* #ifndef APP-NVUE */
  width: 100%;
  height: 100%;
  /* #endif */
  flex: 1;

  ::v-deep .uni-searchbar__box {
    justify-content: flex-start;
  }

  .select-title {
    font-size: 32rpx;
    font-weight: 500;
    color: #666666;
    line-height: 45rpx;
    padding: 20rpx 30rpx 30rpx;
  }

  .select-all-wrap {
    display: flex;
    font-size: 32rpx;
    font-weight: 500;
    color: #333333;
    padding: 30rpx;
  }

  .select-icon {
    width: 40rpx;
    height: 40rpx;
    margin-right: 30rpx;
  }
}

// 处理抽屉内容滚动
.scroll-view-box {
  flex: 1;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  height: calc(100vh - 200rpx);
}

.info {
  padding: 15px;
  color: #666;
}

.info-text {
  font-size: 14px;
  color: #666;
}

.info-content {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 24rpx 20rpx 24rpx 30rpx;

  .dept-icon-wrap {
    background: #f4f5f9;
    border-radius: 50%;
    width: 80rpx;
    height: 80rpx;
    display: flex;
    align-items: center;
    justify-content: center;
    margin-right: 30rpx;

    .dept-icon {
      width: 44rpx;
      height: 36rpx;
    }
  }

  .dept-name {
    font-size: 32rpx;
    font-weight: 500;
    color: #333333;
  }

  .user-num {
    font-size: 32rpx;
    font-weight: 500;
    color: #999999;
  }
}

.close {
  padding: 10px;
}
</style>
